package com.lw.extraceim.ui.fragments;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lw.extraceim.R;
import com.lw.extraceim.app.ExtraceApplication;
import com.lw.extraceim.loader.UserLoader;
import com.lw.extraceim.model.UserInfo;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.ui.activity.LoginActivity;
import com.lw.extraceim.ui.activity.base.BaseActivity;
import com.lw.extraceim.ui.activity.customer.CustomerListActivity;
import com.lw.extraceim.ui.activity.user.UserInfoActivity;
import com.lw.extraceim.ui.fragments.base.BaseFragment;

import okhttp3.Call;

public class MeFragment extends BaseFragment implements View.OnClickListener {
    private ImageButton mImageButtonSetting, mImageButtonCustomer, mImageButtonHistory;
    private Button mButtonExit;

    private RelativeLayout mCustomerLayout;

    public MeFragment(Context mContext, ExtraceApplication application) {
        super(mContext, application);
    }

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.layout_me_fragment, null);
        mImageButtonSetting = view.findViewById(R.id.imagebutton_setting);
        mCustomerLayout = view.findViewById(R.id.customer_layout);
        mImageButtonCustomer = view.findViewById(R.id.imagebutton_customer);
        mImageButtonHistory = view.findViewById(R.id.imagebutton_send_history);
        mButtonExit = view.findViewById(R.id.btn_exit);


        return view;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        mImageButtonHistory.setOnClickListener(this);
        mImageButtonCustomer.setOnClickListener(this);
        mCustomerLayout.setOnClickListener(this);
        mImageButtonSetting.setOnClickListener(this);
        mButtonExit.setOnClickListener(this);
    }

    @Override
    public void refreshUI() {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.imagebutton_setting:
                intent = new Intent(mContext, UserInfoActivity.class);
                break;
            case R.id.imagebutton_send_history:
                break;
            case R.id.customer_layout:
            case R.id.imagebutton_customer:
                intent = new Intent(mContext, CustomerListActivity.class);
                break;
            case R.id.btn_exit:
                intent = new Intent(mContext, LoginActivity.class);
                break;
        }
        if (intent != null)
            startActivity(intent);
    }

    /**
     * 退出登录
     */
    public void logout(){
        UserInfo userInfo = application.getUserInfo();
        UserLoader userLoader = new UserLoader(getContext());
        userLoader.logout(userInfo.getuId(), new HttpResponseParamCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
//                alert("网络错误" + e.getMessage());
            }

            @Override
            public void onResponse(HttpResponseParam response, int id) {

            }
        });
        application.setUserInfo(null);
        application.setSessionId(null);
        LoginActivity.actionStart(getContext());
    }
}
